package com.huaiwei.usmManager.modules.usm.controller;

import com.huaiwei.usmManager.common.utils.R;
import com.huaiwei.usmManager.modules.usm.entity.ShopEntity;
import com.huaiwei.usmManager.modules.usm.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺管理")
public class ShopController {
    @Autowired
    private ShopService shopService;

    @PostMapping("/update")
    @ApiOperation("添加或修改店铺")
    public R update(@RequestBody ShopEntity shop) {
        shopService.insertOrUpdate(shop);
        return R.ok();
    }

    @GetMapping("/list")
    @ApiOperation("店铺分页查询")
    public R list(@RequestParam Map<String,Object> params){
        return R.ok().put("page",shopService.queryPage(params));
    }

    @GetMapping("/info/{shopId}")
    @ApiOperation("根据id获取店铺列表")
    public R info(@PathVariable("shopId") Integer shopId){
        return R.ok().put("list",shopService.getInfoByShopId(shopId));
    }

    @PostMapping("/status")
    @ApiOperation("修改店铺状态")
    public R status(@RequestBody ShopEntity shop){
        shopService.saveOrUpdate(shop);
        return R.ok();
    }

    @DeleteMapping("/delete/{shopId}")
    @ApiOperation("删除店铺")
    public R delete(@PathVariable("shopId") Integer shopId){
       if(shopService.findCabinetInShopByShopId(shopId)){
           shopService.removeById(shopId);
           return R.ok();
       }else{
           return R.error("店铺里还有展示柜!");
       }
    }

    @GetMapping("/cabinet/list/{shopId}")
    @ApiOperation("店铺包含的展示柜列表")
    public R cabinetList(@PathVariable("shopId") Integer shopId,@RequestParam Map<String,Object> params){
        return R.ok().put("page",shopService.getCabinetListByShopId(shopId,params));
    }
}
